% Test some code in terms of simulated MLE
clear
clc
% person, period, drugid, choice, copay, prev


% ignore serial correlations
% focus on class 2

% sketch
% fminsearch @ (t) SMLE(t, data, random_draws)
% SMLE function: simulate using random draws scaled by parameters
% compute a total likelihood of seeing data as a function of parameters
% Compute a Hessian of the function at optimal parameters



% personid quarter drugid chosen cost_sharing incumbent control_extra treat_extra
% starts in June 1996 (1)
% can go to May 2006 (40)
A = importdata('general_entry_choice_data_modified.txt');

data = A(:,1:6);
id = A(:,1);
quarter = A(:,2);
drugid = A(:,3);
chosen = A(:,4);
cost_sharing = A(:,5);
incumbent_id = A(:,6);

% % filtering
filter = (quarter <= 40) .* (drugid < 4) .* (1-isnan(cost_sharing)); % for now, filter based on brand, pre-2006
id(filter == 0, :) = [];
quarter(filter == 0, :) = [];
drugid(filter == 0, :) = [];
chosen(filter == 0, :) = [];
cost_sharing(filter == 0, :) = [];
incumbent_id(filter == 0, :) = [];
data(filter == 0, :) = [];


% extra parameters: number of periods, etc.
num_drugs = max(drugid);
num_periods = max(quarter);
num_people = max(id);

% setup work common to each loop
[~,~,id] = unique([id quarter], 'rows'); % unique person/quarter combos => creates choice id
outside = 1 - accumarray(id, chosen); % chose the outside option


% draw some normal random numbers
rng(17)
S = 100;
random_draws = normrnd(0,1,num_people*num_drugs, S);

% parameters delta for each drug x period, alpha, gamma, sigma2 (of idiosyncratic)
% start with values that roughly reflect previous estimates
%delta_init = [-1;-0.5;-2];
delta_init = [-1.0970;-0.0839;-1.7948];
%params_init = [delta_init; 0.04; 5; 1];
gamma_init = [6.29 0 0; 0 5.56 0; 0 0 7.22];
params_init = [delta_init; 0.0284; gamma_init(:,1);gamma_init(:,2);gamma_init(:,3);-1.391];
params = params_init;

myopts = optimset('TolFun', 1e-10, 'MaxFunEvals', 12000, 'MaxIter',10000, 'display','iter'); % off, iter, final, notify
% previously 10^-8

% test
SMLE_v2(params_init,data,random_draws,num_drugs,num_periods,id,outside,S);

[params_opt, mle] = fminsearch (@(t)...
    -SMLE_v2(t,data,random_draws,num_drugs,num_periods,id,outside,S), params_init, myopts);


delta = params_opt(1:3);

alpha = params_opt(4);
gamma = [params_opt(5:7) params_opt(8:10) params_opt(11:13)];
sigma = params_opt(14);

alpha
gamma
sigma



%% Compute standard errors
fun = @(t) -SMLE_v2(t,data,random_draws, num_drugs, num_periods, id, outside, S);


% hessian of a function at the final point
%[H_small, H, H_big, G] = own_hessian_v2(fun, params_opt);
[H_small, H, H_big, G] = own_hessian_v3(fun, params_opt);

n=length(params_opt);
for i=1:n
    for j=i+1:n
        H(i,j) = H(j,i);
        H_small(i,j) = H_small(j,i);
        H_big(i,j) = H_big(j,i);
    end
end

%variance_mat = inv(H_big);
variance_mat = inv(H_small);
standard_errors = diag(variance_mat.^(.5));



se_gamma = [standard_errors(5:7) standard_errors(8:10) standard_errors(11:13)];
gamma
se_gamma


save('smle_b2.mat');

curr_mle = mle;
cd 'C:\Users\u6025615\Dropbox\Ideas\Organized Ideas\7 - Solo Drafts\History-Dependence and Entry Timing\SMLE'
load smle_column1.mat

diff = curr_mle - mle
